//
//  UINavigationController+VCOrientations.m
//  CLExtension
//
//  Created by CharlieLiao on 2017/7/3.
//  Copyright © 2017年 Charlie. All rights reserved.
//

#import "VCOrientations.h"
#import <objc/runtime.h>


@implementation UINavigationController (VCOrientations)

+ (void)load
{
    Method newImp = class_getInstanceMethod(UINavigationController.class, @selector(cl_pushViewController:animated:));

    Method originImp = class_getInstanceMethod(UINavigationController.class, @selector(pushViewController:animated:));
    
    method_exchangeImplementations(newImp, originImp);
}

- (void)cl_pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [self cl_pushViewController:viewController animated:animated];
    
    // 状态栏方向
    UIDeviceOrientation appOrientation = (UIDeviceOrientation)[UIApplication sharedApplication].statusBarOrientation;
    if (UIDeviceOrientationIsValidInterfaceOrientation(appOrientation)) {
        
        // 判断是否需要旋转
        UIInterfaceOrientationMask supportedOrientations = viewController.supportedInterfaceOrientations;
        BOOL needOrientationFlag = !((1 << appOrientation)&supportedOrientations);
        
        if (needOrientationFlag) {
            // 将要旋转的方向
            UIInterfaceOrientation preferredInterfaceOrientation = viewController.preferredInterfaceOrientationForPresentation;
            
            // 设备方向和将要旋转的方向是一致的，这样会导致没有效果，先让设备旋转到和状态栏一个方向
            if ([UIDevice currentDevice].orientation == (UIDeviceOrientation)preferredInterfaceOrientation)
                [[UIDevice currentDevice] setValue:@(appOrientation) forKeyPath:@"orientation"];
            
            // 开始旋转
            [[UIDevice currentDevice] setValue:@(preferredInterfaceOrientation) forKeyPath:@"orientation"];
        }
    }
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    if ([self.delegate respondsToSelector:@selector(navigationControllerSupportedInterfaceOrientations:)])
        return [self.delegate navigationControllerSupportedInterfaceOrientations:self];
    
    else
        return self.topViewController.supportedInterfaceOrientations;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    if ([self.delegate respondsToSelector:@selector(navigationControllerPreferredInterfaceOrientationForPresentation:)])
        return [self.delegate navigationControllerPreferredInterfaceOrientationForPresentation:self];
    
    else
        return self.topViewController.preferredInterfaceOrientationForPresentation;
}

@end

@implementation UITabBarController (VCOrientations)

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    if ([self.delegate respondsToSelector:@selector(tabBarControllerSupportedInterfaceOrientations:)])
        return [self.delegate tabBarControllerSupportedInterfaceOrientations:self];
    
    else
        return self.selectedViewController.supportedInterfaceOrientations;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    if ([self.delegate respondsToSelector:@selector(tabBarControllerPreferredInterfaceOrientationForPresentation:)])
        return [self.delegate tabBarControllerPreferredInterfaceOrientationForPresentation:self];
    
    else
        return self.selectedViewController.preferredInterfaceOrientationForPresentation;
}

@end
